package com.ktwlsoft.fundmanage.domain.repository;

import com.ktwlsoft.fundmanage.domain.entity.FundSuperviseAccept;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface FundSuperviseAcceptRepository extends JpaRepository<FundSuperviseAccept, Long> {
    boolean existsByTsCode(Long tsCode);

    boolean existsByNetSignCode(String netSignCode);

    FundSuperviseAccept findByTsCode(Long tsCode);

    Page<FundSuperviseAccept> findByTsCodeAndDistrictCodeAndCreatedAtGreaterThanAndCreatedAtLessThanAndAcceptState(long tsCode, String districtCode,
                                                                                                                   Date from, Date to, FundSuperviseAccept.AcceptState acceptState,
                                                                                                                   Pageable pageable);

    @Modifying
    @Query("update FundSuperviseAccept f set f.printTimes = :times, f.acceptState = 'HANDING', f.contractApproval=:contractApproval where f.tsCode = :tsCode")
    int setPrintTimesFor(long tsCode, int times, FundSuperviseAccept.ContractApproval contractApproval);

    @Modifying
    @Query("update FundSuperviseAccept f set f.contractApproval = :approval,f.acceptState='HANDING', f.contractAuditor = :auditor, f.contractAuditorId = :auditorId, f.remark = :remark where f.tsCode = :tsCode")
    int setContractApproval(long tsCode, FundSuperviseAccept.ContractApproval approval, String remark, String auditor, String auditorId);


    /**
     * @param tsCode           交易标号
     * @param districtCode     区域
     * @param contractApproval 协议审核
     * @param acceptState      状态
     * @param from             时间起
     * @param to               时间止
     * @param pageable         分页信息
     * @return
     */
    Page<FundSuperviseAccept> findByTsCodeAndDistrictCodeAndContractApprovalAndCreatedAtGreaterThanAndCreatedAtLessThanAndAcceptState(Long tsCode, String districtCode,
                                                                                                                                      FundSuperviseAccept.ContractApproval contractApproval,
                                                                                                                                      FundSuperviseAccept.AcceptState acceptState,
                                                                                                                                      Date from, Date to, Pageable pageable);

    List<FundSuperviseAccept> findByNetSignCode(String netSignCode);

    @Modifying
    @Query("update FundSuperviseAccept f set f.amountLeft = f.amountLeft+:amount where f.tsCode = :tsCode")
    int addAmouteLeft(BigDecimal amount, Long tsCode);

    @Modifying
    @Query("update FundSuperviseAccept f set f.amountLeft = f.amountLeft-:amount where f.tsCode = :tsCode")
    int subAmouteLeft(BigDecimal amount, Long tsCode);

    @Modifying
    @Query("update FundSuperviseAccept f set f.refundState = :state where f.tsCode = :tsCode")
    int setRefundState(FundSuperviseAccept.RefundState state, long tsCode);

    @Query(value = "select f.* FROM fund_supervise_accept f where f.accept_state !=:acceptState and f.net_sign_code !=''", nativeQuery = true)
    List<FundSuperviseAccept> findByAcceptStateNotContaining(FundSuperviseAccept.AcceptState acceptState);
}
